#  -*- coding: utf-8 -*-

from pymongo import UpdateOne,ASCENDING, DESCENDING
from monitor.base_monitor import BaseMonitor
from data.finance_report_crawler import FinanceReportCrawler
from data.data_module import DataModule
from util.stock_util import get_all_codes,get_all_indexes_date,calc_negative_diff_dates,multi_computer,get_code_name,get_trading_dates,get_diff_dates,get_sub_industry
from util.database import DB_CONN
import time
import pandas as pd
from datetime import datetime, timedelta
from factor.factor_module import FactorModule

"""
实现趋势过程中的风险值监控
"""


class TrendingRiskMonitor(BaseMonitor):
    def __init__(self):
        BaseMonitor.__init__(self, name='trending_risk')
        self.collection = DB_CONN['trending_risk']
        self.collection.create_index([('name', 1), ('date', 1),('type',1),('origin',1)])

    def monitoring(self, begin_date, end_date):
        dm = DataModule()
        fm = FactorModule()
        """
        计算指定时间范围内板块涨幅排名
        """
        code = '600163'
        begin_date = '2021-07-30'

        if end_date is None:
            end_date = datetime.now().strftime('%Y-%m-%d')

        dates = get_trading_dates(begin_date,end_date)
        #result_df = pd.DataFrame(columns=('vol_ratio'),index=dates)
        pre_begin_date = calc_negative_diff_dates(code,is_index=False,date=begin_date, delta_days=-1)
        df_daily = dm.get_k_data(code, index=False, autype='qfq', begin_date=pre_begin_date, end_date=end_date)
        fm_date_df = fm.get_single_stock_factors(code,'vol_ratio', False, begin_date, end_date)

        #result_df['vol_ratio'] = fm_date_df['vol_ratio_5']
        fm_date_df.to_csv(f'fm_{code}_{begin_date}.csv')
        df_daily.to_csv(f'daliydata_{code}_{begin_date}.csv')


        return

if __name__ == '__main__':
    # 执行因子的提取任务
    #hfq =HfqMAFactor()
    pd.set_option('display.width',500)
    pd.set_option('display.max_columns', 500)
    pd.set_option('display.max_colwidth', 500)
    TrendingRiskMonitor().monitoring('2021-09-02', None)
